// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileLocationHelper.cs" company="Scrum for Team System">
//   None
// </copyright>
// <summary>
//   Defines the FileLocationHelper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ScrumForTeamSystem.Setup.SfTSConfigatorUI
{
    using System;
    using System.IO;

    /// <summary>
    /// Initializes instance of FileLocationHelper
    /// </summary>
    internal static class FileLocationHelper
    {
        /// <summary>
        /// The default meta file path.
        /// </summary>
        private const string DefaultMetaDataFilePath = @"Scrum for Team System\ProcessTemplate\metadata.xml";

        /// <summary>
        /// The default zip file location.
        /// </summary>
        private const string DefaultZipFilePath = @"Scrum for Team System\ProcessTemplate\ProcessTemplate.zip";

        /// <summary>
        /// Tries to get the default meta file path.
        /// </summary>
        /// <param name="metaDataFilePath">The meta data file path.</param>
        /// <returns><c>True</c> if the defautl file can be found; otherwise <c>false</c></returns>
        public static bool TryGetDefaultMetaFilePath(out string metaDataFilePath)
        {
            metaDataFilePath = Path.Combine(ProgramFilesx86(), DefaultMetaDataFilePath);

            return File.Exists(metaDataFilePath);
        }

        /// <summary>
        /// Tries to get the default zip file path.
        /// </summary>
        /// <param name="zipFilePath">The meta data file path.</param>
        /// <returns><c>True</c> if the defautl file can be found; otherwise <c>false</c></returns>
        public static bool TryGetDefaultZipFilePath(out string zipFilePath)
        {
            zipFilePath = Path.Combine(ProgramFilesx86(), DefaultZipFilePath);

            return File.Exists(zipFilePath);
        }

        /// <summary>
        /// Find the local Programs files (x86) folder.
        /// </summary>
        /// <returns>The path to the program file folder.</returns>
        private static string ProgramFilesx86()
        {
            if (8 == IntPtr.Size 
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            } 

            return Environment.GetEnvironmentVariable("ProgramFiles");
        }
    }
}